<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>昵称</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="../../plugins/mui/mui.min.css">
		<script src="../../js/statistics.js"></script>
		<style type="text/css">
			 *{
            box-sizing: border-box;
            font-family: 'Microsoft YaHei';
	        }
	        body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, form,  input, p, table, th, td ,select,option{
	            padding: 0;
	            margin: 0;
	            list-style: none;
	        }
	        body{
	        	padding-top:45px;
	        	padding-bottom:10px;
	        }
	        .clearfix:before{
            display:table;
            content:'';
	        }
	        .clearfix:after{
	            display:table;
	            content:'';
	            clear:both;
	        }
			/*title的样式覆盖*/
			.mui-bar{
				background-color:#2a2c33;
				top:0;
			}
			.mui-bar>a{
				color:#fff;
			}
			.mui-title{
				color:#fff;
			}
			.mui-input-row:after{
				display:none;
			}
			.mui-input-group:before{
				display:none;
			}
			.mui-input-group:after{
				display:none;
			}
			#input1::-webkit-input-placeholder{
				line-height: 22px;
			}
			/*百分比优化*/
			.mui-input-clear{
				padding: 11px 4%;
			}
		</style>
	</head>
	<body>
		<header class="mui-bar ">
			<a class=" mui-icon mui-icon-left-nav mui-pull-left" id="back1"></a>
			<h1 class="mui-title">昵称</h1>
			<a  class=" mui-pull-right"  id="save1" style='line-height:44px;font-weight: 500;font-size:15px;'>保存</a>
		</header>
		
		<form class="mui-input-group" style='margin-top:10px;'>
		    <div class="mui-input-row">
		        <input type="text" class="mui-input-clear" placeholder="请输入昵称" style='line-height: 20px;' id='input1' maxlength="6">
		    </div>
		</form>
		<script src="../../plugins/mui/mui.min.js"></script>
		<script src="../../js/getApiToken.js"></script>
		<script src="../../js/md5.js"></script>
		<script type="text/javascript">
			
			mui.plusReady(function(){
//				var self=plus.webview.currentWebview();
//				if(self.name){
//					mui('#input1')[0].value=self.name;
//				}
				
				
				init();
				//初始化是否有昵称
				function init(){
					if(plus.storage.getItem('cg_nickname')){
						input1.value=plus.storage.getItem('cg_nickname');
					}
					
					//关闭等待框
					plus.nativeUI.closeWaiting();
					//显示当前页面
					mui.currentWebview.show('slide-in-right',200);
				}
				
				
				//选择图片后,返回按钮bug
				back1.addEventListener("tap",function () {plus.webview.currentWebview().close();});
				//保存判断用户名函数
				var a1=new RegExp(/^[A-Za-z0-9_\-\u4e00-\u9fa5]+$/);
				var a2=new RegExp(/^-?[1-9]/);
				save1.addEventListener("tap",function () {
					if(input1.value.trim()==''){
						mui.toast('昵称不能为空');
					}else if(!a1.test(input1.value.trim())){
		    			mui.toast('昵称只能为中英文');
		    		}else if(input1.value.trim()==plus.storage.getItem('cg_nickname')){
		    			mui.toast('昵称未修改');
		    		}else if(a2.test(input1.value.trim().substring(0,1))){
		    			mui.toast('昵称不能以数字开头');
		    		}else{
						plus.nativeUI.showWaiting('保存中...');
						
						 myAjax('personal/user_profile/updateUserProfile', 'post', {
								'user_id': plus.storage.getItem('cg_user_id'),
								'user_token': plus.storage.getItem('cg_user_token'),
								'nickname':input1.value.trim()
							}, function(data) {
								plus.nativeUI.closeWaiting();
								if(data.success){
									 //修改本地昵称
									plus.storage.setItem('cg_nickname', input1.value.trim());
									var main = plus.webview.getWebviewById('user.html');//或者通过A页面id获取A页面对象
									mui.fire(main, "pageflowrefresh" ,{'userName':input1.value.trim()});//出发A页面的pageflowrefresh方法
									var main1 = plus.webview.getWebviewById('my-1.html');//或者通过A页面id获取A页面对象
									mui.fire(main1, "pageflowrefresh" ,{'userName':input1.value.trim()});//出发A页面的pageflowrefresh方法
									mui.toast('昵称修改成功');
									plus.webview.currentWebview().close();
								}else{
									mui.toast(data.error_msg);
								}
							})
						
						
						
						

					}
				});
			});
		</script>
	</body>
</html>

